﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using LitJson;

public class MapManager:MonoBehaviour
{
    Transform parent;
    void Awake()
    {
        //NotifacitionCenter.GetInstance().registerObserver(NotifyType.MAPSCENE_LOAD_COMPLETE, on_MAPSCENE_LOAD_COMPLETE);
        //NotifacitionCenter.GetInstance().registerObserver(NotifyType.KITCHEN_SCENE_LOAD_COMPLETE, on_KITCHEN_SCENE_LOAD_COMPLETE);

    }

    void on_MAPSCENE_LOAD_COMPLETE(NotifyEvent e)
    {
        StartCoroutine(InitMapScene());
    }

    void on_KITCHEN_SCENE_LOAD_COMPLETE(NotifyEvent e)
    {
        StartCoroutine(InitKitchenScene());
    }
    
    IEnumerator InitMapScene()
    {
        yield return null;
        
        parent = GameObject.Find("3D/CheckPoints").transform;
        
        List<LevelData> map_list = DataManager.GetInstance().m_MapList.map_list;
        for (int i = 0; i < map_list.Count; i++)
        {
            LevelData levelData = map_list[i];
            //GameObject prefab = (GameObject)Resources.Load("Prefab/CheckPoint");

            GameObject prefab = ResourceManager.dictionary_prefab["assets/prefab/checkpoint.prefab"];

            GameObject go = Instantiate(prefab, prefab.transform.position, Quaternion.identity);
            go.transform.parent = parent;
            go.name = "CheckPoint" + levelData.level1.ToString() + "-" + levelData.level2.ToString();
            go.transform.position = levelData.pos;

            CheckPoint checkPoint = go.GetComponent<CheckPoint>();
            checkPoint.index = i + 1;

            checkPoint.Refresh(levelData);
        }
    }

    IEnumerator InitKitchenScene()
    {
        yield return null;
        
        //UIManager.GetInstance().LoadingPanel.gameObject.SetActive(false);
    }

}
